package com.hqyj.controller;


import com.hqyj.pojo.Orders;
import com.hqyj.pojo.PageData;
import com.hqyj.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    //添加订单
    @RequestMapping("/addOrder")
    @ResponseBody
    public Map<String,Object> addOrder(Integer goodsId,Integer customerId,double price){
        Map<String,Object> result = new HashMap<>();
        //调用服务层参数
        int i = ordersService.addOrder(goodsId, customerId, price);
        System.out.println(i);
        //给视图ajax传递数据
        result.put("code",1);
        result.put("massage","seccess");
        result.put("addOrder",i);
        return result;
    }

    //删除订单
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public Map<String,Object> deleteOrder(Integer id){
        Map<String,Object> result = new HashMap<>();
        int i = ordersService.deleteOrder(id);
        if (i==1){
            result.put("code",1);
            result.put("massage","seccess");
            result.put("deleteOrder",i);
        }else{
            result.put("code",0);
            result.put("massage","fail");
        }
        return result;
    }


    //修改订单状态
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Map<String,Object> updateStatus(Integer id,Integer status){
        Map<String,Object> result = new HashMap<>();
        int i = ordersService.setStatusById(id, status);
        System.out.println(i);
        result.put("code",1);
        result.put("massage","seccess");
        result.put("updateStatus",i);
        return result;
    }


    //获取所有的用户订单
    @RequestMapping("/getAll")
    public String getAllOders(){
        List<Orders> allOders = ordersService.getAllOders();
        System.out.println(allOders);
        return "orders/order";
    }

    @RequestMapping("/getByStatus")
    @ResponseBody
    public Map<String,Object> getOrderByStatus(int status) {
        Map<String, Object> result = new HashMap<>();
        //处理status
        List<Orders> ordersList = ordersService.getOrderByStatus(status);
        result.put("code",1);
        result.put("message","success");
        result.put("data",ordersList);
        System.out.println(result);
        return result;
    }




   //用户查询订单
    @RequestMapping("/myOrder")
    public ModelAndView getMyOrder(String username){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("orders/myOrder");
        List<Map<String, Object>> mapList = ordersService.getUserOrder(username);
        modelAndView.addObject("orderList",mapList);
        System.out.println(mapList);
        return modelAndView;
    }



    @RequestMapping("/getAllByPage")
    public String getAllOrderByPage(Integer pageNumber, Integer pageSize, ModelMap modelMap){
        //处理参数
        int number;
        int size;
        if(pageNumber == null){
            number = 1; //默认显示第1页
        }else{
            number = pageNumber;
        }
        if(pageSize == null){
            size = 5; //默认每页显示两条
        }else{
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Orders> pageData = ordersService.getOrderByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("pageData", pageData);
        //请求转发到success.jsp
        return "orders/order";
    }



    

}
